/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package agency.tango.materialintroscreen.animations;

/**
 * @ProjectName: material-intro-screen-master
 * @Package: agency.tango.materialintroscreen.animations
 * @ClassName: AnimatorValueUtils
 * @Description: 动画工具类
 * @CreateDate: 2021/4/12 16:04
 * @UpdateDate: 2021/4/12 16:04
 * @UpdateRemark: 暂无
 * @Version: 1.0
 */
public class AnimatorValueUtils {
    /**
     * get the animated value with fraction and values
     *
     * @param fraction 0~1
     * @param values   float array
     * @return float animated value
     */
    public static float getAnimatedValue(float fraction, float[] values) {
        if (values == null || values.length == 0) {
            return 0;
        }
        if (values.length == 1) {
            return values[0] * fraction;
        } else {
            if (fraction == 1) {
                return values[values.length - 1];
            }
            float oneFraction = 1f / (values.length - 1);
            float offFraction = 0;
            for (int i = 0; i < values.length - 1; i++) {
                if (offFraction + oneFraction >= fraction) {
                    return values[i] + (fraction - offFraction) * (values.length - 1) * (values[i + 1] - values[i]);
                }
                offFraction += oneFraction;
            }
        }
        return 0;
    }
}
